using System;
using System.Collections;
using System.Web;
using System.Web.UI.WebControls;
using Framework.Library;
using Framework.Library.Components;
using Framework.Library.Components.Portal;
using Framework.Library.Components.Security;
using Framework.Library.Utilities;
using PortalController=Framework.Library.Components.Portal.PortalController;
using Framework.Module.CMS.Library.Zones;
using Framework.Library.Components.Commands;

namespace Portal.Administrator.Portals
{
	public partial class PortalCacheManager : AuthenticatedControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
            lblUpdateStatus.Text = string.Empty;
            if ((!CurrentUser.IsSuperAdmin && CommandController.IsSystemModule(AppEnv.CommandID())) || !CommandController.IsAvailableForUser(AppEnv.CommandID(), CurrentUser.UserID, Convert.ToInt32(AppEnv.PortalId())))
                Response.Redirect(AppEnv.ADMIN_CMD, true);
            if (!IsPostBack)
            {
                //Fill portals                
                dropPortal.Items.Clear();
                PortalController.LoadPortals(dropPortal.Items);
                dropPortal.SelectedValue = AppEnv.PortalId();
                LoadZones();

                pnlCacheAll.Visible = true;
                pnlCacheByPortal.Visible = true;
                btnRemoveAll.Visible = true;
                btnRemoveCache.Visible = true;

                HttpContext context = HttpContext.Current;
                IEnumerator dcEnum = context.Cache.GetEnumerator();
                ArrayList _cacheArray = new ArrayList();
                while (dcEnum.MoveNext())
                    if (((DictionaryEntry)dcEnum.Current).Value != null)
                        _cacheArray.Add(((DictionaryEntry)dcEnum.Current).Key.ToString());
                _cacheArray.Sort();
                lstAllCache.DataSource = _cacheArray;
                lstAllCache.DataBind();
            }
		}

        protected void LoadZones()
        {
            dropZone.Items.Clear();            
            ZoneController.LoadZones(dropZone.Items, dropPortal.SelectedValue);
            dropZone.Items.Insert(0, new ListItem("Root", "0"));
            dropZone_SelectedIndexChanged(null,null);
       }

        protected void dropPortal_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadZones();
        }

		protected void btnRemoveCache_Click(object sender, EventArgs e)
		{
			DataCaching data = new DataCaching();
			foreach(ListItem item in lstCache.Items)
				data.RemoveCache(item.Text);
            lstCache.DataBind();
            lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
		}

		protected void btnRemoveAll_Click(object sender, EventArgs e)
		{
            DataCaching data = new DataCaching();
            foreach (ListItem item in lstAllCache.Items)
               data.RemoveCache(item.Text);
           lstAllCache.DataBind();
            lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
		}
		
        protected void dropZone_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstCache.DataSource = PortalCacheController.GetPortalCacheByPortalIdAndZoneID(ConvertUtility.ToInt32(dropPortal.SelectedValue), ConvertUtility.ToInt32(dropZone.SelectedValue));
            lstCache.DataTextField = "CacheKey";
            lstCache.DataValueField = "Portal_Cache_ID";
            lstCache.DataBind();
        }
	}
}